package com.platform_server.service.impl;

import com.platform_server.config.security.JwtTokenUtil;
import com.platform_server.entity.db.Admin;
import com.platform_server.entity.response.Header;
import com.platform_server.entity.response.QueryResponse;
import com.platform_server.entity.response.ResponseBase;
import com.platform_server.mapper.AdminMapper;
import com.platform_server.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @Author：Kevin.W.Liu
 * @Date: 2022/3/12
 * @Version 1.0
 */
@Service
public class AdminServiceImpl implements IAdminService {
    @Autowired
    private UserDetailsService userDetailsService;
    //将前端传过来的密码进行加密
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public ResponseBase login(String username, String password, HttpServletRequest request) {
        QueryResponse queryResponse = new QueryResponse();
        Header header = new Header();

        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        if (null==userDetails||passwordEncoder.matches(password,userDetails.getPassword())){
            //return Res.error("用户名或密码不正确");
            header.setCode(200);
            header.setMessage("用户名或密码不正确");
            queryResponse.setHeader(header);
            return queryResponse;
        }
        if (!userDetails.isEnabled()){
            //return Res.error("账户被禁用，请联系管理员！");
            header.setCode(500);
            header.setMessage("账户被禁用，请联系管理员!");
            queryResponse.setHeader(header);
            return queryResponse;
        }
        //更新security登录的用户对象
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        //验证成功，生成并返回tonken
        String token = jwtTokenUtil.generateToken(userDetails);
        HashMap<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        tokenMap.put("tokenHead",tokenHead);
        queryResponse.setResult(tokenMap);
        return queryResponse;
    }

    /**
     * 根据用户名获取用户信息
     * @param username
     * @return
     */
    @Override
    public Admin getAdminByUserName(String username) {

        return adminMapper.selectActiveAdminByName(username);
    }
}
